﻿using AutoMapper;
using MediatR;
using MyMES.Domain.zyy;
using MyMES.Error;
using MyMES.Infrastructure.Interface;
using MyMES.Writer.API.Application.Commends.Productionwork;

namespace MyMES.Writer.API.Application.CommendHandlers.Productionwork
{
    public class CreateProductionworkCommendHandlers : IRequestHandler<CreateProductionworkCommends, APIResult<WorkOrder>>
    {

        private readonly IBaseRepository<WorkOrder> repository;
        private readonly IMapper mapper;

        public CreateProductionworkCommendHandlers(IBaseRepository<WorkOrder> repository, IMapper mapper)
        {
            this.repository = repository;
            this.mapper = mapper;
        }


        /// <summary>
        /// 创建生产工单
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        /// 
       
        public async Task<APIResult<WorkOrder>> Handle(CreateProductionworkCommends request, CancellationToken cancellationToken)
        {
            APIResult<WorkOrder> result = new APIResult<WorkOrder>();
            result.Code = APIEnum.添加成功;
            result.Msg = "添加成功";
            var model = mapper.Map<WorkOrder>(request);
            string sql = "insert  into WorkOrder (WorkOrderId,WorkOrderCoding,WorkOrderName,WorkOrderType,WorkorderSource,OrderNumber,ProductNumber,ProductName,Specifications,Unit,WorkOrderNum,AdjustQuantity,QuantityProduced,LotNumber,CustomerCode,CustomerNamwe,RequestDate,State,SupplierCode,SupplierName,Note,IsDlete) values (@WorkOrderId,@WorkOrderCoding,@WorkOrderName,@WorkOrderType,@WorkorderSource,@OrderNumber,@ProductNumber,@ProductName,@Specifications,@Unit,@WorkOrderNum,@AdjustQuantity,@QuantityProduced,@LotNumber,@CustomerCode,@CustomerNamwe,@RequestDate,@State,@SupplierCode,@SupplierName,@Note,@IsDlete)";

            await repository.Insert(model, sql);
            return result;
        }
    }
}
